#include <tenacitas_log/text_file.h>

using namespace tenacitas::log;


// #############################################################################
// text_file_printer

// -----------------------------------------------------------------------------
text_file_printer::
text_file_printer( const std::string & p_dir_log,
                   const std::string & p_pgm_name,
                   int64_t p_pid,
                   std::thread::id p_th_id ) :
    m_file( ) {
    std::stringstream l_stream;
    l_stream << p_dir_log << "/" << p_pgm_name << "_" << p_pid << "_";
    if ( p_th_id == std::thread::id( ) ) {
        l_stream << "0";
    }
    else {
        l_stream << p_th_id;
    }

    l_stream << ".log";
    const std::string l_log_file_name = l_stream.str( );

    m_file = std::shared_ptr<ofstream>( new ofstream( l_log_file_name.c_str( ),
                                                      std::ios::out ) );
    ( *m_file ) << "text-format v1";
}

// -----------------------------------------------------------------------------
void
text_file_printer::
new_line( level p_level, const char * p_file,
          const char * p_function, int64_t p_line ) {
    ( *m_file ) << std::endl << p_level.str( ) << "|"
                << std::chrono::nanoseconds ( std::chrono::
                                              high_resolution_clock::now().
                                              time_since_epoch() ).count( )
                << "|" << p_file
                << "|" << p_function
                << "|" << p_line
                << "|";
}


// #############################################################################
// text_file_printer_factory

// -----------------------------------------------------------------------------
text_file_printer_factory::
text_file_printer_factory( const std::string & p_dir_log,
                           const std::string & p_pgm_name,
                           int64_t p_pid )
    : m_dir_log( p_dir_log ),
      m_pgm_name( p_pgm_name ),
      m_pid( p_pid ) {}
